﻿using Liuliu.ScriptEngine;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GmPrizedrawTool
{
    /// <summary>
    /// 登陆游戏
    /// </summary>
    internal class LoginGame
    {
        /// <summary>
        /// 执行操作
        /// </summary>
        /// <param name="Hwnd">程序句柄</param>
        /// <param name="actionType">操作类型(1、监控BOSS死亡  2、系统抽奖  3、移动玩家)</param>
        /// <param name="moveUserNum">移动玩家数量，类型为移动玩家时使用</param>
        /// <param name="movePoint">移动坐标，类型为移动玩家时使用</param>
        public static void StartLogin(int Hwnd,int actionType, int? moveUserNum = 0,string ? movePoint="")
        {
            DmPlugin dm = Program.dm;
            int pid = Program.golobalProcess.Id;
            Thread.Sleep(20);
            //绑定窗口
            bool isBind = dm.BindWindowEx(Hwnd, "gdi", "windows", "windows", "", 0);//绑定窗体
            if (isBind)
            {
                //循环获取当前程序窗口，判断当前窗口所属页面
                int intX, intY;
                int Seconed = 0;
                Console.WriteLine("开始获取游戏窗口");
                while (true)
                {
                    #region 自动登录流程
                    //判断是否登录报错弹框
                    int dm_ret = dm.FindPic(451, 415, 586, 456, "Config\\loginerr.bmp", "000000", 0.8, 0, out intX, out intY);
                    if (dm_ret != -1)
                    {
                        dm.MoveTo(495, 443);
                        Thread.Sleep(100);
                        dm.LeftClick();
                        Thread.Sleep(100);
                        dm.MoveTo(945, 515);
                        Thread.Sleep(100);
                        dm.LeftClick();
                        Thread.Sleep(100);
                    }
                    //是否登录页面
                    dm_ret = dm.FindPic(912, 695, 938, 709, "Config\\login.bmp", "000000", 0.8, 0, out intX, out intY);
                    if (dm_ret != -1)
                    {
                        //登录操作
                        dm.MoveTo(876, 352);
                        Thread.Sleep(100);
                        dm.LeftClick();
                        Thread.Sleep(100);//等待页面响应

                        dm.MoveTo(889, 425);
                        dm.KeyPress(64);//清空账号
                        Thread.Sleep(100);
                        dm.SendString(Hwnd, Program.UserName);

                        Thread.Sleep(200);//等待页面响应
                        dm.MoveTo(873, 453);
                        Thread.Sleep(100);
                        dm.LeftClick();
                        Thread.Sleep(100);
                        dm.SendString(Hwnd, Program.UserPwd);

                        Thread.Sleep(200);
                        dm.MoveTo(850, 508);
                        Thread.Sleep(100);
                        dm.LeftClick();
                        continue;
                    }
                    //是否角色选择页面
                    dm_ret = dm.FindPic(0, 721, 71, 763, "Config\\char.bmp", "000000", 0.8, 0, out intX, out intY);
                    if (dm_ret != -1)
                    {
                        //取消弹框
                        dm.MoveToClick(542, 415);
                        Thread.Sleep(100);
                        //选择角色
                        int paddingWidth = 135;
                        intX = 65 + (paddingWidth * (1 - 1));
                        intY = 740;

                        dm.MoveTo(intX, intY);
                        Thread.Sleep(100);
                        dm.LeftClick();
                        Thread.Sleep(100);//等待页面响应

                        dm.MoveTo(945, 583);
                        Thread.Sleep(100);
                        dm.LeftClick();
                        continue;
                    }
                    #endregion
                    //判断是否游戏主界面
                    dm_ret = dm.FindPic(0, 740, 40, 768, "Config\\game.bmp", "000000", 0.8, 0, out intX, out intY);
                    if (dm_ret != -1)
                    {
                        Console.WriteLine("进入了游戏主界面");
                        //设置隐身
                        dm.SendString(Hwnd, $"-hide");
                        Thread.Sleep(200);
                        dm.KeyPress(13);//回车发送指令
                        //点击喇叭只保留世界频道内容
                        #region MyRegion
                        dm.MoveToClick(30,738);//打开喇叭
                        Thread.Sleep(50);
                        dm.MoveToClick(38, 614);//一般
                        Thread.Sleep(50);
                        dm.MoveToClick(38, 630);//公开
                        Thread.Sleep(50);
                        dm.MoveToClick(38, 646);//密语
                        Thread.Sleep(50);
                        dm.MoveToClick(38, 662);//队伍
                        Thread.Sleep(50);
                        dm.MoveToClick(38, 678);//诸侯
                        Thread.Sleep(50);
                        dm.MoveToClick(38, 694);//国家
                        Thread.Sleep(50);
                        dm.MoveToClick(38, 726);//公告
                        Thread.Sleep(50);
                        dm.MoveToClick(180, 640);//关闭喇叭
                        Thread.Sleep(50);
                        #endregion

                        if (actionType == 1)
                        {
                            //1、监控BOSS死亡
                            Console.WriteLine("当前功能暂未开放");
                            KillBoss.ListionMessageBox(Hwnd); 
                            break;
                        }
                        else if (actionType == 2)
                        {
                            //2、系统抽奖 
                            RandomReward.StartReward(Hwnd); 
                        }
                        else
                        {
                            //3、拉人
                            Console.WriteLine("当前功能暂未开放");
                            MoveUser.StartMoveUser(Hwnd, moveUserNum??0, movePoint);
                            break;
                        }
                    }

                    if (Seconed >= 30000)
                    {
                        Console.WriteLine("游戏登录失败，请重新开始");
                        break;//超过30s视为未启动成功
                    }
                    Seconed += 100;
                    Thread.Sleep(200);//等待软件启动完成
                }
            }
            else
            {
                Console.WriteLine("窗口绑定失败");
            }
        }
    }
}
